<template>
  <div class="container">
    <h1>{{id? '编辑':'添加'}}时间线</h1>
    <el-form :model="model" @submit.native.prevent="save" label-width="120px">
      <el-tabs type="border-card">
        <el-tab-pane label="基础信息">
          <el-button @click="model.content.push({})">
            <i class="el-plus">添加信息</i>
          </el-button>

          <el-row type="flex" style="flex-wrap:wrap">
            <el-col :md="12" v-for="(item,i) in model.content" :key="i">
              <el-form-item label="时间:">
                <el-input v-model="item.time"></el-input>
              </el-form-item>

              <el-form-item label="标题:">
                <el-input v-model="item.title"></el-input>
              </el-form-item>

              <el-form-item label="内容:">
                <el-input placeholder="请输入内容" v-model="item.body1"></el-input>
                <el-input placeholder="请输入内容" v-model="item.body2"></el-input>
                <el-input placeholder="请输入内容" v-model="item.body3"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>

      <el-form-item>
        <el-button type="primary" native-type="submit">{{id? '保存':'添加'}}</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  props: {
    id: {},
  },
  data() {
    return {
      model: {
        content: [],
      },
    };
  },
  methods: {
    async save() {
      // 添加个人信息
      if (!this.id) {
        let res = await this.$http.post("timeline", this.model);
        this.$message({
          type: "success",
          message: "添加个人信息成功",
        });
        this.$router.push("/timelines/list");
        // 编辑个人信息
      } else {
        let res = await this.$http.put(`/timeline/${this.id}`, this.model);
        this.$message({
          type: "success",
          message: "保存成功",
        });
        this.$router.push("/timelines/list");
      }
    },
    // 获取编辑个人信息初始数据
    async fetch() {
      const res = await this.$http.get(`/timeline/${this.id}`);
      this.model = res.data;
    },
  },
  created() {
    this.id && this.fetch();
  },
};
</script>

<style></style>